-- ------------------------------------------------ --
-- this lua file saves debug configurations of nvim --
-- ------------------------------------------------ --
return {
    {
        "rcarriga/nvim-dap-ui",
	    dependencies = { "nvim-neotest/nvim-nio" },
        config = function ()

            -- update debug ui
            -- 自定义高亮组颜色（示例为暗红色断点+黄色当前行）
            vim.api.nvim_set_hl(0, 'DapBreakpoint', { fg = '#ff5555', bg = '#2d3036', bold = true })
            vim.api.nvim_set_hl(0, 'DapStopped', { fg = '#ffd700', bg = '#2d3036', underline = true })

            -- 定义断点符号（使用 Nerd Font 图标）
            vim.fn.sign_define('DapBreakpoint', {
                text = '',  -- 断点图标
                texthl = 'DapBreakpoint',
                linehl = 'DapBreakpointLine',  -- 断点所在行背景色
                numhl = 'DapBreakpointNumber'  -- 行号颜色
            })
            vim.fn.sign_define('DapStopped', {
                text = '',  -- 当前执行行图标
                texthl = 'DapStopped',
                linehl = 'DapStoppedLine',  -- 当前行背景高亮
                numhl = 'DapStoppedNumber'
            })
        end
    },
    {
	"mfussenegger/nvim-dap",
	config = function()
		-- load dap & dap-ui
		local dap, dapui = require("dap"), require("dapui")
        dapui.setup()
		-- bash DAP --
        dap.adapters.bashdb = {
            type = 'executable';
            command = vim.fn.stdpath("data") .. '/mason/packages/bash-debug-adapter/bash-debug-adapter';
            name = 'bashdb';
        }
		dap.configurations.sh = {
			{
				type = "bashdb",
				request = "launch",
				name = "Launch file",
				showDebugOutput = true,
				pathBashdb = vim.fn.stdpath("data") .. "/mason/packages/bash-debug-adapter/extension/bashdb_dir/bashdb",
				pathBashdbLib = vim.fn.stdpath("data") .. "/mason/packages/bash-debug-adapter/extension/bashdb_dir",
				trace = true,
				file = "${file}",
				program = "${file}",
				cwd = "${workspaceFolder}",
				pathCat = "cat",
				pathBash = "/bin/bash",
				pathMkfifo = "mkfifo",
				pathPkill = "pkill",
				args = {},
				env = {},
				terminalKind = "integrated",
			},
		}
		-- bash dap end --

        -- C/C++ gdb native DAP --
        dap.adapters.cppdbg = {
            id = 'cppdbg',
            type = 'executable',
            command = vim.fn.stdpath("data") .. "/mason/bin/OpenDebugAD7",
        }

        dap.configurations.cpp = {
            {
                name = "Launch file",
                type = "cppdbg",
                request = "launch",
                program = function()
                    return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
                end,
                cwd = '${workspaceFolder}',
                stopAtEntry = true,
            },
            {
                name = 'Attach to gdbserver :1234',
                type = 'cppdbg',
                request = 'launch',
                MIMode = 'gdb',
                miDebuggerServerAddress = 'localhost:1234',
                miDebuggerPath = '/usr/bin/gdb',
                cwd = '${workspaceFolder}',
                program = function()
                    return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
                end,
            },
        }
        -- C/C++ DAP end

		-- dap-ui auto open & close
		dap.listeners.before.attach.dapui_config = function()
			dapui.open()
		end
		dap.listeners.before.launch.dapui_config = function()
			dapui.open()
		end
		dap.listeners.before.event_terminated.dapui_config = function()
			dapui.close()
		end
		dap.listeners.before.event_exited.dapui_config = function()
			dapui.close()
		end
	end,
}
}
